# -*- coding: utf-8 -*-
# from odoo import api, fields, models
from odoo import api, fields, models


class Vehicle(models.Model):
    """车辆
    fleet.vehicle.model 车型 原模型有
    fleet.vehicle.model.brand 品牌 原模型有
    原模型中owner_id是车主
    """

    _name = 'tour.vehicle'
    _description = u'车辆'
    _inherit = 'fleet.vehicle'
    _table = 'fleet_vehicle'

    owner_id = fields.Many2one('tour.owner', string=u'车主')
    property = fields.Selection(selection=[('service', u'营运'), ('non-operating', u'非营运')], string=u'使用性质')
    identify_number = fields.Char(string=u'车辆识别号')
    engine_number = fields.Char(string=u'发动机号')
    people_count = fields.Integer(string=u'核定载人数')
    buy_date = fields.Date(string=u'购车日期')
    car_img1 = fields.Char(string=u'车辆照片1')
    car_img2 = fields.Char(string=u'车辆照片2')
    driving_car = fields.Char(string=u'注册类别')


class VehicleOwner(models.Model):
    """车主"""

    _name = 'tour.owner'
    _description = u'车主'
    _inherit = 'tour.travel.partner'
    _table = 'res_partner'

    license = fields.Char(string=u'经营许可证')
    driving_licence = fields.Char(string=u'驾驶证号码')
    address = fields.Char(string=u'住址')
    vehicle_ids = fields.One2many('tour.vehicle', 'owner_id', string=u'车辆')

    @api.model
    def create(self, values):
        result = super(VehicleOwner, self).create(values)
        result.role = 'owner'

        # 创建净车价产品
        product_vals = {
            'owner_id': result.id,
            'product_id': self.env.ref('tour.travel_product_other').id
        }
        self.env['tour.owner.product'].create(product_vals)

        return result

    @api.multi
    def action_approve(self):
        for owner in self:
            if owner.state == 'to approve':
                owner.state = 'done'

    @api.multi
    def action_disapprove(self):
        for owner in self:
            if owner.state == 'to approve':
                owner.state = 'disapprove'


class VehicleDriver(models.Model):
    """司机"""

    _name = 'tour.driver'
    _description = u'司机'
    _inherit = 'tour.travel.partner'
    _table = 'res_partner'

    driving_licence = fields.Char(string=u'驾驶证号码')
    address = fields.Char(string=u'住址')

    @api.model
    def create(self, values):
        result = super(VehicleDriver, self).create(values)
        result.role = 'driver'
        return result
